<!--
 * @Description: 宠物品种管理
 * @Author: 龙春雨
 * @Date: 2023-04-11 10:15:19
-->
<template>
  <basic-container>
    <avue-crud
      ref="crud"
      v-model="form"
      :search.sync="searchForm"
      :data="tableData"
      :table-loading="tableLoading"
      :option="tableOption"
      :before-open="beforeOpen"
      :before-close="beforeClose"
      :upload-error="uploadError"
      :upload-change="$uploadChange"
      @on-load="getList"
      @search-change="searchChange"
      @search-reset="searchReset"
      @row-update="handleUpdate"
      @row-save="handleSave"
      @row-del="handleDelete"
    >
      <template v-slot:parentCodeForm>
        <avue-select
          v-model="form.parentCode"
          placeholder="请选择内容"
          :dic="firstData"
          :disabled="true"
          :props="{ label: 'name', value: 'code' }"
        ></avue-select>
      </template>
      <template v-slot:name="{ row }">{{ row.label }}</template>
      <template v-slot:code="{ row }">{{ row.value }}</template>
      <template v-slot:remark="{ row }">{{ row.extra.remark }}</template>
      <template v-slot:sort="{ row }">{{ row.extra.sort }}</template>
      <template v-slot:hotFlag="{ row }">
        {{ row.extra.hotFlag === IS_HOT.YES ? '是' : '否' }}
      </template>
      <template v-slot:createTime="{ row }">
        {{ row.extra.createTime }}
      </template>
      <template v-slot:pic="{ row }">
        <el-avatar
          v-if="row.extra.pic"
          shape="square"
          :size="30"
          fit="contain"
          :src="row.extra.pic"
        ></el-avatar>
      </template>
      <template v-slot:menu="{ row }">
        <el-button
          v-if="row.parent === '0'"
          size="small"
          type="text"
          @click="addChildrenChange(row)"
        >
          新增
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { tableOption } from '@/const/crud/pet/classifyMng';
import {
  addObj,
  delObj,
  getTree,
  putObj,
  getListByCode,
} from '@/api/core/classify';
import crudMixins from '@/mixins/crud';
import { IS_HOT } from '@/const/type';
export default {
  name: 'classifyMng',
  mixins: [crudMixins],
  data() {
    return {
      form: {},
      IS_HOT: Object.freeze(IS_HOT),
      parentCode: '',
      firstData: [],
    };
  },
  computed: {
    tableOption() {
      return tableOption(this.form.parentCode);
    },
  },
  methods: {
    uploadError(error, column) {
      if (error === '文件太大不符合') {
        this.$message.warning(`文件大小不能超过${column.fileSize / 1024}M`);
      }
    },
    //文件大小
    // uploadBefore(file, done, loading, column) {
    //   // column.fileSize = 5 * 1024;
    //   console.log(file, column.fileSize, file.size > column.fileSize * 1024);
    //   if (file.size > column.fileSize * 1024) {
    //     loading();
    //     this.$message.warning(`上传的文件不能超过${column.fileSize / 1024}M`);
    //   } else {
    //     done();
    //   }
    async getFirstData() {
      try {
        const { data } = await getListByCode('0');
        this.firstData = data.data;
      } catch (error) {
        throw new Error(error);
      }
    },
    // 获取列表
    async getList() {
      try {
        this.tableLoading = true;
        const { data } = await getTree({
          ...this.searchForm,
        });
        this.tableData = data.data;
        this.tableLoading = false;
      } catch (error) {
        this.tableLoading = false;
        throw new Error(error);
      }
    },
    // 编辑
    async handleUpdate(row, index, done, loading) {
      try {
        await putObj(row);
        done();
        this.getList();
      } catch (error) {
        loading();
        throw new Error(error);
      }
    },
    // 保存
    async handleSave(row, done, loading) {
      try {
        await addObj(row);
        done();
        this.getList();
      } catch (error) {
        loading();
        throw new Error(error);
      }
    },
    // 删除
    handleDelete: function (row, index) {
      this.$confirm('是否确认删除名称为' + row.label + '的宠物分类!', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return delObj(row.id);
        })
        .then(() => {
          this.$message.success('删除成功');
          this.getList();
        });
    },
    addChildrenChange(row) {
      this.parentCode = row.value;
      this.$refs.crud.rowAdd();
    },
    // 打开前的拦截
    async beforeOpen(done, type) {
      await this.getFirstData();
      if (type !== 'add') {
        this.form.name = this.form.label;
        this.form.code = this.form.value;
        this.form.remark = this.form.extra.remark;
        this.form.sort = this.form.extra.sort;
        this.form.pic = this.form.extra.pic;
        this.form.hotFlag = this.form.extra.hotFlag;
        delete this.form.label;
        delete this.form.value;
      } else {
        this.form.parentCode = this.parentCode;
      }

      if (type === 'edit') {
        this.form.parentCode = this.form.parent;
      }
      done();
    },
    beforeClose(done) {
      this.parentCode = '';
      done();
    },
  },
};
</script>

<style lang="scss">
.dialog-classify {
  .el-collapse-item__header {
    margin-bottom: 0;
  }
  .avue-upload.avue-upload--list {
    position: absolute;
    width: 100%;
    text-align: left;
  }
  .avue-upload__avatar {
    width: 100px;
    height: 100px;
    line-height: 100px !important;
  }
}
</style>
